﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using System.Windows;
using DataAccessLayer;
using MyException;

namespace Maintain_supplier_information
{
    public partial class MaintainSupplierInfo : System.Web.UI.Page
    {
        SupplierBizlogic sbl;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId != 7)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                sbl = new SupplierBizlogic();

                if (!IsPostBack)
                {
                    lbl_status.Text = "";
                    ClearFields();
                }

                // Block the buttons to prevent multiple clicks by the user
                btn_submit.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btn_submit).ToString() + ";");

                lbl_status.Text = "";
            }
            catch (Exception ex)
            {
                btn_submit.Enabled = true;
                btn_cancel.Enabled = true;
            }
        } 

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            lbl_status.Text = "";

            List<string> st = new List<string>();

            try
            {
                string supplierId = txt_supplierid.Text.Trim();

                st = sbl.getDepId();
                if (st.Contains(supplierId))
                {
                    //string duplicateError = "A supplier with Supplier ID \"" + supplierId + "\" already exist";
                    ////ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Duplication of values')", true);
                    //lbl_status.Text = duplicateError;
                    throw new DuplicateSupplierException();
                }
                string phoneNo = txt_phoneno.Text.Trim();
                string faxNo = txt_faxno.Text.Trim();

                string supplierName = txt_suppliername.Text.Trim();
                string contactName = txt_contactname.Text.Trim();
                int phoneNoDigit;
                if (phoneNo.Equals(""))
                {
                    phoneNoDigit = 0;
                }
                else
                {
                    phoneNoDigit = Convert.ToInt32(phoneNo);
                }
                int faxNoDigit;
                if (faxNo.Equals(""))
                {
                    faxNoDigit = 0;
                }
                else
                {
                    faxNoDigit = Convert.ToInt32(faxNo);
                }
                string gst = txt_gstregno.Text.Trim();
                string address = txt_address.Text.Trim();
                string email = txt_email.Text.Trim();

                if (supplierId.Equals("") || supplierName.Equals("") || contactName.Equals("") || phoneNoDigit == 0 || faxNoDigit == 0 || address.Equals("") || gst.Equals("") || email.Equals(""))
                {
                    throw new MissingRequiredFieldsException();
                }

                Supplier supplier = new Supplier();
                supplier.SupplierID = supplierId;
                supplier.SupplierName = supplierName;
                supplier.ContactName = contactName;
                supplier.PhoneNo = phoneNoDigit;
                supplier.FaxNo = faxNoDigit;
                supplier.Address = address;
                supplier.GSTRegNo = gst;
                supplier.Email = email;

                sbl.SaveNewSupplier(supplier);

                lbl_status.Text = supplierName + " has been added successfully";
                ClearFields();
                btn_cancel.Enabled = true;
                btn_submit.Enabled = true;

                #region TEST save supplier
                //bool ans = false;

                //string phoneNo = txt_phoneno.Text.Trim();
                //string faxNo = txt_faxno.Text.Trim();

                //string supplierName = txt_suppliername.Text.Trim();
                //string contactName = txt_contactname.Text.Trim();
                //int phoneNoDigit;
                //if (phoneNo.Equals(""))
                //{
                //    phoneNoDigit = 0;
                //}
                //else
                //{
                //    phoneNoDigit = Convert.ToInt32(phoneNo);
                //}
                //int faxNoDigit;
                //if (faxNo.Equals(""))
                //{
                //    faxNoDigit = 0;
                //}
                //else
                //{
                //    faxNoDigit = Convert.ToInt32(faxNo);
                //}
                //string gst = txt_gstregno.Text.Trim();
                //string address = txt_address.Text.Trim();
                //string email = txt_email.Text.Trim();

                //ans = sbl.savesupplier(supplierId, supplierName, contactName, phoneNoDigit, faxNoDigit, gst, address, email);
                //if (ans == true)
                //{
                //    lbl_status.Text = supplierName + " has been added";
                //    ClearFields();
                //    btn_cancel.Enabled = true;
                //    btn_submit.Enabled = true;
                //}
                //else
                //{
                //    lbl_status.Text = "Please contact the IT department";
                //    btn_cancel.Enabled = true;
                //    btn_submit.Enabled = true;
                //}
                #endregion 
            }
            catch (DuplicateSupplierException ex)
            {
                lbl_status.Text = "Supplier ID already exist";
                btn_cancel.Enabled = true;
                btn_submit.Enabled = true;
            }
            catch (MissingRequiredFieldsException ex)
            {
                btn_cancel.Enabled = true;
                btn_submit.Enabled = true;
            }
            catch (Exception ex)
            {
                btn_cancel.Enabled = true;
                btn_submit.Enabled = true;
            }
        }

        protected void btn_cancel_Click(object sender, EventArgs e)
        {
            try
            {
                lbl_status.Text = "";
                ClearFields();
            }
            catch (Exception ex)
            {
            }
        }

        private void ClearFields()
        {
            txt_supplierid.Text = "";
            txt_suppliername.Text = "";
            txt_contactname.Text = "";
            txt_phoneno.Text = "";
            txt_faxno.Text = "";
            txt_gstregno.Text = "";
            txt_address.Text = "";
            txt_email.Text = "";
        } 
    }
}









